package com.teamb6.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.teamb6.shop.domain.Goods;
import com.teamb6.shop.domain.GoodsExample;
import com.teamb6.shop.domain.ImagePath;
import com.teamb6.shop.domain.ImagePathExample;
import com.teamb6.shop.mapper.GoodsMapper;
import com.teamb6.shop.mapper.ImagePathMapper;
import com.teamb6.shop.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 业务层：商品
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired(required = false)
    GoodsMapper goodsMapper;

    @Autowired(required = false)
    ImagePathMapper imagePathMapper;

    //展示商品列表
    @Override
    public List<Goods> listGoods(String keyword) {
        keyword="%"+keyword+"%";
        GoodsExample example = new GoodsExample();
        example.or().andGoodsnameLike(keyword);
        return goodsMapper.selectByExample(example);
    }

    //删除选中的单上商品
    @Override
    public int deleteGoodsByPrimaryKey(Integer id) {
        return goodsMapper.deleteByPrimaryKey(id);
    }

    //删除选中的多行商品
    @Override
    public int multiDelGoods(String ids) {
        GoodsExample example = new GoodsExample();
        List<String> list = Arrays.asList(ids.split(","));
        List<Integer> ilist = list.stream().map(Integer::parseInt).collect(Collectors.toList());
        example.or().andGoodsidIn(ilist);
        return goodsMapper.deleteByExample(example);
    }

    //获取选中的商品的详细信息
    @Override
    public Goods findGoodsByPrimaryKey(Integer id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    //更新修改后的商品信息
    @Override
    public int updateGoods(Goods goods) {
        return goodsMapper.updateByPrimaryKeySelective(goods);
    }

    //添加商品信息
    @Override
    public Integer addGoods(Goods goods) {
        goodsMapper.insertSelective(goods);
        return goods.getGoodsid();
    }
    //将图片路径存到数据库中
    @Override
    public void addImagePath(ImagePath imagePath) {
        imagePathMapper.insertSelective(imagePath);
    }

    @Override
    public List<Goods> selectByExample(GoodsExample example) {
        return goodsMapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public void deleteGoodsById(Integer goodsid) {

        goodsMapper.deleteByPrimaryKey(goodsid);
    }

    @Override
    public void updateGoodsById(Goods goods) {
        goodsMapper.updateByPrimaryKeySelective(goods);
    }

    @Override
    public List<ImagePath> findImagePath(Integer goodsid) {
        ImagePathExample imagePathExample = new ImagePathExample();
        imagePathExample.or().andGoodidEqualTo(goodsid);

        return imagePathMapper.selectByExample(imagePathExample);
    }

    @Override
    public Goods selectById(Integer goodsid) {
        return goodsMapper.selectByPrimaryKey(goodsid);
    }

    //活动，图片，商品三表查询
    @Override
    public List<Goods> getActivityGoods() {
        return goodsMapper.getActivityGoods();
    }
    //查询所有正在进行活动的商品
    @Override
    public List<Goods> selectAllByActivity(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectAllByActivity();
    }

    //根据分类id查询
    @Override
    public List<Goods> getByCategoryId(Integer cateId, Integer pageNum , Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.getByCategoryId(cateId);
    }
    //根据时间查询
    @Override
    public List<Goods> getByTime() {
        return goodsMapper.selectByTime();
    }
    //根据时间查询所有
    @Override
    public List<Goods> getAllByTime(Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectAllByTime();
    }

    //根据时间查询所有
    @Override
    public List<Goods> getAllByHot(Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectAllByHot();
    }
    //根据价格查询所有（低到高）
    @Override
    public List<Goods> getHotByLowPrice(Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectHotByLowPrice();
    }
    //根据价格查询所有（高到低）
    @Override
    public List<Goods> getHotByHighPrice(Integer pageSize,Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectHotByHighPrice();
    }
    //按价格查询所有（低到搞）(活动商品)
    @Override
    public List<Goods> getActivityByLowPrice(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectActivityByLowPrice();
    }
    //按价格查询所有（搞到低）(活动商品)
    @Override
    public List<Goods> getActivityByHighPrice(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectActivityByHighPrice();
    }

    //按价格查询所有（低到高）(分类商品)
    @Override
    public List<Goods> getByCategoryIdLow(Integer categoryId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectCateByLowPrice(categoryId);
    }
    //按价格查询所有（高到低）(分类商品)
    @Override
    public List<Goods> getByCategoryIdHigh(Integer categoryId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectCateByHighPrice(categoryId);
    }
    //所有商品价格排序
    @Override
    public List<Goods> getAllsByLowPrice(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectAllsByLowPrice();
    }
    //所有商品价格排序
    @Override
    public List<Goods> getAllsByHighPrice(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.selectAllsByHighPrice();
    }


    //按热度查询
    @Override
    public List<Goods> getByHot(){
        return goodsMapper.selectByHot();
    }
//
//    @Override
//    public List<Goods> selectByName(String goodsName) {
//        return goodsMapper.selectByName(goodsName);
//    }

    //根据订单号多表查询订单中的商品
    @Override
    public List<Goods> getIndentGoods(Integer orderid) {
        return goodsMapper.getByOrderid(orderid);
    }


    @Override
    public List<Goods> findAllProducts(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.pagesPart();
    }

    @Override
    public List<Goods> mySelectByName(String goodsName,Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return goodsMapper.mySelectByName(goodsName);
    }

}
